// pages/user/index.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('buttonwx.getUserInfo'),
    userInfo: {
      avatar: '',
      nickName: '',
    },
    account: [
      { name: '礼品卡', value: 0 },
      { name: '余额', value: 0 },
      { name: '红包', value: 0 },
      { name: '优惠券', value: 0 },
      { name: '津贴', value: 0 },
    ],
    order: [
      { name: '待付款', icon: 'iconfont icon-tubiao-' },
      { name: '待发货', icon: 'iconfont icon-daifahuo' },
      { name: '待收货', icon: 'iconfont icon-daishouhuo' },
      { name: '待评价', icon: 'iconfont icon-message1' },
      { name: '退款/售后', icon: 'iconfont icon-tuikuan1' },
    ],
    service: [
      { name: '官方客服', icon: 'iconfont icon-kefu' },
      { name: '我的积分', icon: 'iconfont icon-jifen' },
      { name: '我要反馈', icon: 'iconfont icon-fankui' },
      { name: '我的拼团', icon: 'iconfont icon-pintuan' },
      { name: '地址管理', icon: 'iconfont icon-ziyuan' },
      { name: '账号安全', icon: 'iconfont icon-zhanghaoanquan' },
      { name: '我的拍卖', icon: 'iconfont icon-paimai' },
      { name: '加群领红包', icon: 'iconfont icon-tianjiaqunzu_huaban1' },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAccount();
    this.getStorage();
  },

  clickCode() {
    console.log('clickCode');
  },
  getStorage() {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo !== '') {
      let { nickName, avatarUrl } = userInfo;
      this.setData({
        userInfo: {
          avatar: avatarUrl,
          nickName: nickName,
        },
      });
    }
  },
  getAccount() {
    const { account } = this.data;
    for (let i = 0; i < account.length; i++) {
      let accoutItem = account[i]
      if (accoutItem.name === '余额' || accoutItem.name === '津贴') {
        if ((accoutItem.value).toString().match('￥') === null)
        accoutItem.value = '￥' + accoutItem.value.toFixed(2);
      }
    }
    this.setData({
      account,
    });
  },
  login() {
    wx.navigateTo({
      url: '../login/index',
    });
  },
  logout() {
    const that = this;
    wx.showModal({
      content: '确定退出登录？',
      success(res) {
        if (res.confirm) {
          app.globalData.userInfo = null;
          wx.removeStorageSync('userInfo');
          that.setData({
            userInfo: {
              avatar: '',
              nickName: '',
            },
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let info = app.globalData.userInfo;
    if (info !== null) {
      let { avatarUrl, nickName } = info;
      this.setData({
        userInfo: {
          avatar: avatarUrl,
          nickName: nickName,
        },
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAccount();
    this.getStorage();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
